/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "announceresponse.h"

#include "bencoder.h"
#include "bdictnode.h"
#include "blistnode.h"
#include "bvaluenode.h"

#include <QtCore>

AnnounceResponse::AnnounceResponse(int _interval, int _complete, int _incomplete)
		: interval(_interval), complete(_complete), incomplete(_incomplete)
{
	//HARDCODED!
	m_compact = true;
}

AnnounceResponse::~AnnounceResponse()
{}

void AnnounceResponse::addPeer(const QString& id, const QHostAddress & ip, const quint16 port)
{
	Peer p;
	p.ip = ip.toString();
	p.peerID = id;
	p.port = port;

	peerMap.append(p);
}

QByteArray AnnounceResponse::toString() const
{
	BEncoder enc;
	enc.beginDict();
	{
		//key
		enc.writeString("interval");
		//value
		enc.writeUInt(30);

		/*
		//key
		enc.write("min interval");
		//value
		enc.write((quint32)15);*/

		//key
		enc.writeString("complete");
		//value
		enc.writeUInt((quint32) complete);

		//key
		enc.writeString("incomplete");
		//value
		enc.writeUInt((quint32) incomplete);

		//key
		enc.writeString("peers");

		//value
		if (m_compact)
		{
			//Kompakt response: STRING 4 bajta HOST i 2 bajta PORT
			QByteArray peers;

			Peer p;
			foreach(p, peerMap)
			{
				QStringList list = p.ip.split('.');

				// 4-byte IP adresa
				peers.append(list[0].toInt());
				peers.append(list[1].toInt());
				peers.append(list[2].toInt());
				peers.append(list[3].toInt());
				
				// 2-byte PORT
				quint16 port = p.port;				
				char* b = (char*) &port;
				
				peers.append((char) *(b+1));
				peers.append((char) *b);
			}

			enc.writeString(peers);
		}
		else
		{
			enc.beginList();
			{
				Peer p;
				foreach(p, peerMap)
				{
					/* omit peer id
					//key
					enc.write("peer id");
					//value
					enc.write(p.peerID);
					*/

					//key
					enc.writeString("ip");
					//value
					enc.writeString(p.ip.toAscii());

					//key
					enc.writeString("port");
					//value
					enc.writeUInt((quint32) p.port);
				}
			}
			enc.end();
		}
	}
	enc.end();

	return *enc.getData();
}
